#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "lt_unit.h"
#include "lt_config.h"
extern int errno;

FILE *popen(const char *command, const char *mode);
int pclose(FILE *stream);

lt_boolean print_cmd_execute(char *cmd)
{
	char res_line[MAX_RESULT_LENGTH] = "";
	
	FILE *pipe = popen(cmd, "r");
	if ( pipe == NULL )
	{
		printf("open pipe failed with cmd %s\n", cmd);
		return lt_false;
	}
	
	while ( fgets(res_line, sizeof res_line, pipe) )
	{
		printf("%s\n", res_line);
		memset(res_line, 0, sizeof res_line);
	}
	pclose(pipe);

	pipe = popen("echo $?", "r");
	if ( fgets(res_line, sizeof res_line, pipe) )
	{
		if ( atoi(res_line) != 0 )
		{
			printf("execute cmd faield with %s\n", cmd);
			return lt_false;
		}
	}
	pclose(pipe);

	return lt_true;
}

void print_sys_error()
{
	fprintf(stderr, "Errno: %d\n", errno);
    fprintf(stderr, "Errstr: %s\n", strerror(errno));
}